package com.yulebaby.callsenter.ui.view;

import android.graphics.Color;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.yulebaby.callsenter.bean.ActualTranProgressBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2017/1/22
 */
public class ActualCountColumn {

    public final static String[] stepTarget = new String[]{"步骤一", "步骤二", "步骤三", "步骤四", "步骤五", "步骤六", "步骤七"};
    private BarChart mChart;

    // 设置图表样式
    public ActualCountColumn(BarChart chart) {
        this.mChart = chart;
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.animateXY(1000, 1000);
        mChart.setDescription("");
        mChart.setNoDataText("");
        mChart.setMaxVisibleValueCount(60);

        mChart.setPinchZoom(false);

        mChart.setDrawGridBackground(false);

        // 设置是否可以触摸
        mChart.setTouchEnabled(false);
        // 是否可以拖拽
        mChart.setDragEnabled(false);
        // 是否可以缩放
        mChart.setScaleEnabled(false);
        // 集双指缩放
        mChart.setPinchZoom(false);
        // 隐藏右边的坐标轴
        mChart.getAxisRight().setEnabled(false);
        // 隐藏左边的左边轴
        mChart.getAxisLeft().setEnabled(false);


        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);

        Legend l = mChart.getLegend();
        l.setEnabled(false);
        mChart.invalidate();
    }

    public void setData(List<ActualTranProgressBean> result) {

        ArrayList<BarEntry> yVals1 = new ArrayList<>();

        for (int i = 0; i < 7; ++i) {
            for (int j = 0; j < result.size(); j++) {
                ActualTranProgressBean bean = result.get(j);
                if ((bean.getTrack_pro() - 1) == i) {
                    yVals1.add(new BarEntry((float) bean.getActualNum(), i));
                } else if (j == result.size()) {
                    yVals1.add(new BarEntry(0, i));
                }
            }
        }

        BarDataSet set1;


        set1 = new BarDataSet(yVals1, "实际转化");
        set1.setColor(Color.parseColor("#1798FB"));
        set1.setBarSpacePercent(60f);

        ArrayList<BarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);

        BarData data = new BarData(Arrays.asList(stepTarget), dataSets);
        data.setValueFormatter(value -> (int) value + "");
        data.setValueTextSize(10f);

        mChart.setData(data);
    }

}
